package com.kun.reflection.demo02_memoryAnalysis;

import org.junit.Test;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

//反射操作泛型
public class Test07_operateGenerics {

    public void test01(List<String> list, Map<String,Integer> map){
        System.out.println("test01");
    }

    public Map<String,Integer> test02(){
        System.out.println("test02");
        return null;
    }

    @Test
    public void test() throws Exception {
        //反射操作泛型
        Class c1 = Test07_operateGenerics.class;

        //1. 方法参数--泛型
        Method method = c1.getMethod("test01", List.class, Map.class);
        Type[] types_generic = method.getGenericParameterTypes();//获取方法参数的泛型类型
        for (Type type : types_generic) {
            System.out.println("## 参数类型：" + type.getTypeName());
            if (type instanceof ParameterizedType){
                Type[] types = ((ParameterizedType) type).getActualTypeArguments();
                for (Type t : types) {
                    System.out.println("泛型---类型：" + t.getTypeName());
                }

            }
        }

        System.out.println("----------");

        //2. 方法返回值--泛型
        method = c1.getMethod("test02", null);
        Type type = method.getGenericReturnType();//获取方法返回参数的泛型类型
        System.out.println("## 参数类型：" + type.getTypeName());
        if (type instanceof ParameterizedType){
            Type[] types = ((ParameterizedType) type).getActualTypeArguments();
            for (Type t : types) {
                System.out.println("泛型---类型：" + t.getTypeName());
            }

        }
    }
}
